﻿using System.Web.Http;
using Dapper;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Collections.Generic;
using Ninject;
using Common.JWT;

namespace WebApiWithMs.Controllers
{
    public class ProductsController : ApiController
    {
        [Inject]
        public IJsonToken token { get; set; }
        [Inject]
        public ITeamRepository team { get; set; }
        private readonly string _connectionString;
        //这个应该配置
        private const string key = "f73759308164deaff02d8302f256a4a8";
        //测试用例
        private readonly Member member = new Member { name = "爱吃肉的小怪兽", email = "daichen1017@vip.qq.com" };
        public ProductsController()
        {
            _connectionString = ConfigurationManager.AppSettings["UsedConnectionString"];
        }
        [Route("v1/products")]
        public IEnumerable<dynamic>  GetProducts()
        {
            using (var connetion = new MySqlConnection(_connectionString))
            {
                var list = connetion.Query<dynamic>("SELECT * FROM team LIMIT 10");
                return list;
            }
        }
        [Route("v1/product")]
        public Team GetProduct()
        {
            return team.Get("1");
        }
        [Route("v1/sign")]
        public string GetSign()
        {
            return token.Encode(member, key);
        }
    }

    public class Member
    {
        public string name { get; set; }

        public string email { get; set; }
    }
}
